<?php
namespace Home\Controller;
use Think\Controller;
use Org\Net;
use Think\Model;
use Home\Model\WeixinModel;
class UserController extends Controller{
    
	//个人中心
	public function cen(){
	    $openid=I("get.openid");
	    if(!empty($openid)){
	        cookie("openid",$openid);
	    }
		$list=M('client')->where(array('openid'=>$openid))->find();
		if(!isset($list["headimg"])|| empty($list["headimg"]) ){
		    $list["headimg"]=C("host")."/".PUBLIC_PATH."img/default.jpg";
		}
		$this->assign('list',$list);
		$this->assign('openid',$openid);
		$this->display();
	}
	//排行榜
	public function pai(){
		$user = M('client');
		$users=$user->field('uname,score,headimg')->order('score desc')->select();
		foreach ($users as &$value){
    		if(empty($value["headimg"])){
    		    $value["headimg"]=C("host")."/".PUBLIC_PATH."img/default.jpg";
    		}
		}
		$this->assign('users',$users);
		$this->display();
	}
	
	//邀请好友页面
	public function yao(){
	    $openid=I("get.openid");
	    if(!empty($openid)){
	        cookie("openid",$openid);
	    }else{
	        $openid=cookie("openid");
	    }
            $lineLink="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".C("appid")."&redirect_uri=".urlencode(C("host").U("Index/oauth2",array("openid"=>$openid)))."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    	    $appid=C("appid");
    	    $this->assign("jifen",C("yaoqingscore"));
    	    $this->assign("lineLink",$lineLink);
    	    $this->assign("descContent","限量抢试用资格，下载应用即可免费获取Q币、话费，支付宝。每个月轻松赚取几百块。妈妈再也不用担心我的话费啦");
    	    $this->assign("imgurl",C("host")."/Public/img/default.jpg");
    	    $this->assign("appid",$appid);
    	    $this->assign("openid",$openid);
    		$this->display();
	}

	/**
	 * 收入查询页面
	 */
	public function shou(){ 
	    $openid=I("get.openid");
	    $yq=0;//邀请收入
	    $xj=0;//试玩收入
	    $yt=0;//已提收入	
	    $kt=0;//可提收入 
	    $yt=M('inte')->where(array('openid'=>$openid,"type"=>array(0,2),"biao"=>array(1,2,3,"or")))->Sum("score");
	    $list=M('client')->where(array('openid'=>$openid))->find();
	    if(!isset($list["headimg"])|| empty($list["headimg"]) ){
	        $list["headimg"]=C("host").PUBLIC_PATH."img/default.jpg";
	    }
	    $xj=M("note")->where(array('userid'=>$openid))->Sum("cash");
	    $yq=M('yaoqing')->where(array('openid'=>$openid))->Sum("score");
	    $allscore=$list["score"]?$list["score"]:0;
	    $kt=$allscore;
	    if(empty($xj)){
	        $xj=0;
	    }
	    if(empty($yt)){
	        $yt=0;
	    }
	    if(empty($yq)){
	        $yq=0;
	    }
	    $this->assign('kt',$kt);//可提收入
	    $this->assign('yt',$yt);//已提收入
	    $this->assign('suo',$allscore);//所有收入
	    $this->assign('xj',$xj);//试玩收入
	    $this->assign('yq',$yq);//邀请收入
	    $this->assign("list",$list);
	    $this->assign("openid",$openid);
		$this->display();
	}
	//填写支付信息页面
	public function tixian(){
		$openid = I('get.openid');		
		$this->assign('openid',$openid);
		$this->display();
	}
    //提交支付信息
	public function zhifu(){
		$openid=I("get.openid"); 
		$da['dhscore']=$data['jin']=I('post.jin');
		$score=$da['score']=$data['score']=I('post.jin')*XIANJIN;
		$data['name']=I('post.shoukuanren');
		$data['zfb']=I('post.zhanghao');
		$da['openid']=$data['openid']=$openid;
		$cc = M('client')->where(array('openid'=>$openid))->find();
		$da['cid']=$data['cid']=$cc['uid'];
		$da['type']=1;//未完成
		$data['biao']=$da['biao']=1; 
		$da['times']=date('Y-m-d',time());
		$da["biaoname"]="提现";
		$scor =M('client')->where(array('openid'=>$openid))->find();
		if($scor['score'] < $score){
			$this->error('积分不足，不能兑换！');
		}else{
			$arr=M('zfb')->add($data);
			M('inte')->add($da);
			if($arr){
				$this->success('提交成功，等待管理员处理！',U('user/tixian'));
			}else{
				$this->error('提交失败，请重新提交！',U('user/tixian'));
			}
		}	

	}
	
	//手机归属地查询
	public function getArea($phone){
		$sms = array('province'=>'', 'supplier'=>'' );    //初始化变量
		//根据淘宝的数据库调用返回值
		$url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$phone;
		 
		$content = file_get_contents($url);
		$sms['province'] = substr($content, "56", "4");  //截取字符串
		$sms['supplier'] = substr($content, "81", "4");
		return $sms;
	}	
		
	//兑换话费
	public function dhhf(){
		header("Content-Type:text/html;charset=utf8");
        if(!empty($_POST)){
        	$jin=I('post.jin');
        	$phone=I('post.phone'); 
        	$content = $this->getArea($phone);
	      	$yun = iconv('gbk', 'UTF-8', $content['supplier']);
        	$sheng = iconv('gbk', 'UTF-8', $content['province']);

        	$vo=M('chong')->where(array('cityid'=>$sheng,'operators'=>$yun))->find();
        	$number=$vo['number'];
        	//检查积分是否达到兑换量
        	$score=$jin*HUAFEI;
        	$scor =M('client')->where(array('openid'=>$openid))->find();
        	if($scor['score'] < $score){
        		$this->error('积分不足，不能兑换！');
        	}else{
        		$da['score']=$scor['score']-$score;
        		$pp =M('client')->where(array('openid'=>$openid))->save($da);
        		//接入久久
        		$_99dou = new \Org\Net\_99douInterface();
        		$out_trade_id = uniqid(mt_rand(), true);
        		$msg = "";
        		//手机充值
        		$result = $_99dou->Direct($out_trade_id,$number,1,$phone,"",$_SERVER["REMOTE_ADDR"],5,$msg);
        		if($result == 0){
        			//添加兑换记录
        			$data['type']='2';
        			$data['biao']='3';
        			$data['dhscore']=$jin;
        			$data['score']=$score;
        			$data['openid']=I("get.openid");
        			$data['cid']=$scor['uid'];
        			$data['times']=date('Y-m-d H:i',time());
        			$data["biaoname"]="兑换话费";
        			$arr =M('inte')->save($data);
        			if($arr){
        				$this->success('充值成功！',U('User/dhhf'));
        			}else{
        				$this->error('充值成功,状态更改失败！',U('User/dhhf'));
        			}
        		}else{
        			$this->error('充值失败！',U('User/dhhf'));
        		}
        	}
        	
        	
        }else{
        	$list=M('ctiy')->select();
        	$this->assign('list',$list);
        	$this->display();
        }
		
	}
	
	
	//兑换Q币
	public function dhqb(){
		if(!empty($_POST)){
			$jin=I('post.jin');
			$qq=I('post.qq');
			$vo=M('qb')->where(array('qb'=>1))->find();
			$number=$vo['number'];
			//检查积分是否达到兑换量
			$score=$jin*QBI;
			$scor =M('client')->where(array('openid'=>$openid))->find();
			if($scor['score'] < $score){
				$this->error('积分不足，不能兑换！');
			}else{
				$da['score']=$scor['score']-$score;
				$pp =M('client')->where(array('openid'=>$openid))->save($da);
				//接入久久
				$_99dou = new \Org\Net\_99douInterface();
				$out_trade_id = uniqid(mt_rand(), true);
				$msg = "";
			//充值QB
				
				$result = $_99dou->Direct($out_trade_id,$number, intval($jin),$qq,"",$_SERVER["REMOTE_ADDR"],5,$msg);
	        	//$result = $_99dou->Direct($out_trade_id,$number, 1,$qq,"",$_SERVER["REMOTE_ADDR"],5,$msg);
	        	if($result == 0){
	        		//添加兑换记录
	        		$data['type']='2';
	        		$data['biao']='2';
	        		$data['dhscore']=$jin;
	        		$data['score']=$score;
	        		$data['openid']=I("get.openid");
	        		$data['cid']=$scor['uid'];
	        		$data['times']=date('Y-m-d H:i',time());
	        		$data["biaoname"]="兑换QB";
	        		$arr =M('inte')->save($data);
	        		if($arr){
	        			$this->success('充值成功！',U('User/dhqb'));
	        		}else{
	        			$this->error('充值成功,状态更改失败！',U('User/dhqb'));
	        		}        		
	        	}else{
	        		$this->error('充值失败！',U('User/dhqb'));
	        	}
			}	
		}else{
			
			$this->display();
		}
	}
	
	/**
	 * 试玩记录
	 */
	public function shiwanjilu(){
	    $openid = I('get.openid');
	    $sql="select * from ".C("DB_PREFIX")."client t ,".C("DB_PREFIX")."note g where t.openid=g.openid and t.openid=".$openid;
	    $mode=new \Think\Model();
	    $list=$mode->query($sql);
	    $emptyinfo="";
	    if(is_array($list) && count($list)){
	      $this->assign("list",$list);
	    }else{
	        $emptyinfo="试玩记录为空！";
	        $this->assign("emptyinfo",$emptyinfo);
	    }
	    $this->display();
	}
	
	/**
	 * 邀请记录
	 */
	public function yaoqingjilu(){
	     $openid = I('get.openid');
	     $sql="select * from ".C("DB_PREFIX")."client t ,".C("DB_PREFIX")."yaoqing g where t.openid=g.shangxian and t.openid=".$openid;
	     $mode=new \Think\Model();
	     $list=$mode->query($sql);
	     $emptyinfo="";
	     if(is_array($list) && count($list)){
	        $this->assign("list",$list);
	     }else{
	        $emptyinfo="邀请记录为空！";
	        $this->assign("emptyinfo",$emptyinfo);
	    }
	     $this->display();
	}
	
	/**
	 * 提现记录
	 */
	public function tixianjilu(){
	    $openid = I('get.openid');
	    $sql="select * from ".C("DB_PREFIX")."client t ,".C("DB_PREFIX")."inte g where t.openid=g.openid and t.openid=".$openid;
	    $mode=new \Think\Model();
	    $list=$mode->query($sql);
	    $emptyinfo="";
	    if(is_array($list) && count($list)){
	        $this->assign("list",$list);
	    }else{
	        $emptyinfo="提现记录为空！";
	        $this->assign("emptyinfo",$emptyinfo);
	    }
	    $this->assign("list",$list);
	    $this->display();
	}
}